Java xxList.stream().collect(Collectors.groupingBy(User::getCategory)); List对象集合按对象属性分组、分组汇总、过滤等操作示例

您所在的位置:网站首页 element can not be mapped to a null key Java xxList.stream().collect(Collectors.groupingBy(User::getCategory)); List对象集合按对象属性分组、分组汇总、过滤等操作示例

Java xxList.stream().collect(Collectors.groupingBy(User::getCategory)); List对象集合按对象属性分组、分组汇总、过滤等操作示例

2024-05-30 23:21| 来源: 网络整理| 查看: 265

userList.stream().collect(Collectors.groupingBy(User::getCategory));使用详解及扩展。直接上代码(底部附带异常) @Test void contextLoads1() { User user0 = new User(); user0.setId(0); user0.setNickname("name0"); user0.setUsername("username0"); user0.setCategory("学生"); User user1 = new User(); user1.setId(1); user1.setNickname("name1"); user1.setUsername("username1"); user1.setCategory("老师"); User user2 = new User(); user2.setId(2); user2.setNickname("name2"); user2.setUsername("username2"); user2.setCategory("教学主任"); List userList = new ArrayList(); userList.add(user0); userList.add(user1); userList.add(user2);     //跟据某个属性分组 Map userMap = userList.stream().collect(Collectors.groupingBy(User::getCategory)); System.out.println(userMap);     // 查询结果  // {学生=[User{id=0, nickname=name0, birthday=null, username=username0, password=null, status=null, createTime=null, updateTime=null, remarks=null}], // 老师=[User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null}], // 教学主任=[User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]}     //根据某个属性分组,汇总某个属性    Map collect2 = userList.stream().collect(Collectors.groupingBy(User::getCategory,Collectors.summingInt(User::getId)));    System.out.println(collect2);    // 查询结果    // {学生=0, 老师=1, 教学主任=2}

    //根据某个属性添加条件过滤数据     userList = userList.stream().filter(u -> !u.getNickname().equals("name0")).collect(Collectors.toList());    System.out.println(userList);    // 查询结果    // [User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null},    // User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]     //判断一组对象里面有没有这个属性值     boolean add = userList.stream().anyMatch(m -> "name2".equals(m.getNickname()));    System.out.println(add);

    //取出一组对象的某个属性组成一个新集合    List Username=userList.stream().map(User::getUsername).collect(Collectors.toList());    System.out.println(Username);    // 查询结果      // [username0, username1, username2]

User实体类

public class User implements Serializable { private static final long serialVersionUID=1L; /** * 主键 */ private Integer id; /** * 昵称 */ private String nickname; /** * 类别 */ private String category; /** * 生日 */ private LocalDate birthday; /** * 用户名 */ private String username; /** * 密码 */ private String password; /** * 状态 */ private String status; /** * 创建时间 */ private LocalDateTime createTime; /** * 更新时间 */ private LocalDateTime updateTime; /** * 备注 */ private String remarks;   // 省略set/get }

 element cannot be mapped to a null key 

 / / 按照 User 分组,此时 getCategory 有空值,就会报错。

 // 改为,增加非空过滤。

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3